package com.oa.controller;

import com.oa.back.ResponseBack;
import com.oa.common.annotion.Interceptor;
import com.oa.common.enums.CodeStatus;
import com.oa.common.vo.RSAVo;
import com.oa.service.CommonService;
import com.oa.service.FileService;
import com.oa.service.RSAService;
import org.apache.tomcat.util.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.interfaces.RSAPublicKey;

@Controller
@RequestMapping(value = "/common")
public class CommonController extends BaseController {

    @Autowired
    private CommonService commonService;

    @Autowired
    private RSAService rsaService;

    @Autowired
    private FileService fileService;

    private static final Logger logger= LoggerFactory.getLogger(CommonController.class);


    @RequestMapping(value = "get_img.html",method = RequestMethod.GET)
    public void getImg(HttpServletRequest request, HttpServletResponse servletResponse){
        try {
            commonService.createImage(request,servletResponse);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "public_key",method = RequestMethod.GET)
    @ResponseBody
    public RSAVo publicKey(HttpServletRequest request){
        RSAPublicKey publicKey=rsaService.generateKey(request);
        RSAVo vo= new RSAVo();
        vo.setModulus(Base64.encodeBase64String(publicKey.getModulus().toByteArray()));
        vo.setExponent( Base64.encodeBase64String(publicKey.getPublicExponent().toByteArray()));
        return vo;
    }

    @RequestMapping(value = "get_token",method = RequestMethod.GET)
    @ResponseBody
    public String getToken(HttpServletRequest request){
        return commonService.getToken(request);
    }

    @RequestMapping(value = "upload",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBack<String> uploadImage(MultipartFile file, int type, Boolean isShear, Boolean isRotato){
        try {
            if (isRotato==null)
                isRotato= false;
            if (isShear==null)
                isShear=false;
            return ResponseBack.success(fileService.upload(file,type,isShear,isRotato,0));
        } catch (IOException e) {
            logger.error("上传图片的异常:",e);
            return ResponseBack.fial(CodeStatus.SYSTEM_FAIL.getResponseStatus());
        }
    }

    @RequestMapping(value = "uploadBase64",method = RequestMethod.POST)
    @ResponseBody
    @Interceptor(actionName = "base64上传图片")
    public ResponseBack<String> uploadBase(String base64,int type,Boolean isShear,Boolean isRote){
        try {
            if (isRote==null){
                isRote= false;
            }
            if(isShear==null)
                isShear=false;
            return ResponseBack.success(fileService.upload(base64,type,isShear,isRote,0));
        } catch (IOException e) {
            logger.error("上传图片的异常:",e);
            return ResponseBack.fial(CodeStatus.SYSTEM_FAIL.getResponseStatus());
        }
    }

}
